package minijvm.cmd;

import minijvm.clz.ClassFile;
import minijvm.constant.FieldRefInfo;
import minijvm.engine.ExecutionResult;
import minijvm.engine.Heap;
import minijvm.engine.JavaObject;
import minijvm.engine.StackFrame;
import org.apache.commons.lang3.StringUtils;

/**
 * @author ChangLiang
 * @date 2020/4/26
 */
public class GetStaticFieldCmd extends TwoOperandCmd {
    public GetStaticFieldCmd(ClassFile clzFile, String opCode) {
        super(clzFile, opCode);
    }

    @Override
    public void execute(StackFrame frame, ExecutionResult result) {
        FieldRefInfo info = (FieldRefInfo) this.getConstantInfo(this.getIndex());
        String className = info.getClassName();
        String fieldName = info.getFieldName();
        String fieldType = info.getFieldType();
        if (StringUtils.equals("java/lang/System", className)
                && StringUtils.equals("out", fieldName)
                && StringUtils.equals("Ljava/io/PrintStream", fieldType)
        ) {
            JavaObject jo = Heap.getInstance().newObject(className);
            frame.getOperandStack().push(jo);
        }
    }
}
